﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Modelo.Modulos.ClasesModelado;

namespace AccesoDatos
{
    public class ClientesBD
    {
        public void Insertar(SqlCommand comando, ClientesMod obj)
        {
            try
            {
                string sp = "spInsertarCliente";

                comando.CommandType = CommandType.StoredProcedure;
                comando.CommandText = sp;
                comando.Parameters.Clear();

               
                comando.Parameters.Add(new SqlParameter("@Nombre", SqlDbType.VarChar));
                comando.Parameters.Add(new SqlParameter("@Descripcion", SqlDbType.VarChar));

               
                comando.Parameters[0].Value = obj.Nombre;
                comando.Parameters[1].Value = obj.Descripcion;
               

                comando.ExecuteNonQuery();

                
            }
            catch (Exception exc)
            {
                throw exc;
            }
        }

        public void Eliminar(SqlCommand comando, ClientesMod obj)
        {
            try
            {
                string sp = "spEliminaCliente";

                comando.CommandType = CommandType.StoredProcedure;
                comando.CommandText = sp;
                comando.Parameters.Clear();


                comando.Parameters.Add(new SqlParameter("@IdCliente", SqlDbType.Int));
             
                comando.Parameters[0].Value = obj.IdCliente;
               
                comando.ExecuteNonQuery();


            }
            catch (Exception exc)
            {
                throw exc;
            }
        }

        public List<ClientesMod> Consultar(SqlCommand comando)
        {
            try
            {
                string sp = "spConsultarCliente";

                comando.CommandType = CommandType.StoredProcedure;
                comando.CommandText = sp;
                comando.Parameters.Clear();

                IDbDataAdapter da = new SqlDataAdapter((SqlCommand)comando);
                DataSet ds = new DataSet();
                da.Fill(ds);

                DataTable dt = ds.Tables[0];
                List<ClientesMod> lista = new List<ClientesMod>();

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ClientesMod obj = new ClientesMod();
                    obj.IdCliente = Convert.ToInt32(dt.Rows[i]["IdCliente"]);
                    obj.Nombre = dt.Rows[i]["NombreCliente"].ToString();
                    obj.Descripcion = dt.Rows[i]["DescripcionCliente"].ToString();
                    lista.Add(obj);
                }


                return lista;
                
            }
            catch (Exception exc)
            {
                throw exc;
            }
        }

        public List<ClientesMod> ConsultarClienteID(SqlCommand comando,ClientesMod objs)
        {
            try
            {
                string sp = "spConsultarClientebyId";

                comando.CommandType = CommandType.StoredProcedure;
                comando.CommandText = sp;
                comando.Parameters.Clear();

                comando.Parameters.Add(new SqlParameter("@IdCliente", SqlDbType.VarChar));

                comando.Parameters[0].Value = objs.IdCliente;

                IDbDataAdapter da = new SqlDataAdapter((SqlCommand)comando);
                DataSet ds = new DataSet();
                da.Fill(ds);

                DataTable dt = ds.Tables[0];
                List<ClientesMod> lista = new List<ClientesMod>();

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ClientesMod obj = new ClientesMod();
                   
                    obj.Nombre = dt.Rows[i]["NombreCliente"].ToString();
                    obj.Descripcion = dt.Rows[i]["DescripcionCliente"].ToString();
                    lista.Add(obj);
                }


                return lista;

            }
            catch (Exception exc)
            {
                throw exc;
            }
        }

        public void ActualizarCliente(SqlCommand comando, ClientesMod obj)
        {
            try
            {
                string sp = "spActualizarCliente";

                comando.CommandType = CommandType.StoredProcedure;
                comando.CommandText = sp;
                comando.Parameters.Clear();

                comando.Parameters.Add(new SqlParameter("@IdCliente", SqlDbType.VarChar));
                comando.Parameters.Add(new SqlParameter("@Nombre", SqlDbType.VarChar));
                comando.Parameters.Add(new SqlParameter("@Descripcion", SqlDbType.VarChar));

                comando.Parameters[0].Value = obj.IdCliente;
                comando.Parameters[1].Value = obj.Nombre;
                comando.Parameters[2].Value = obj.Descripcion;


                comando.ExecuteNonQuery();


            }
            catch (Exception exc)
            {
                throw exc;
            }
        }

    }
}
